
let setCk = (cname, cvalue, exdays=7) => {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+d.toUTCString()+';path=/';
  document.cookie = cname + "=" + cvalue + "; " + expires;
}

export const setCookie = (cname, cvalue, exdays=7) => {
  setCk(name, "", -1); 
  setCk(cname, cvalue, exdays)
}

export const getCookie = (cname) =>{
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1);
      if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
  }
  return "";
}

export const clearCookie = (name) => {  
  setCk(name, "", -1);  
}

export const isPhone = (phone) => {
  return /^1[3456789]\d{9}$/.test(phone)
}
export const isPwd = (pwd) => {
  return /^(?=.*\d)(?=.*[a-z])[a-zA-Z0-9]{6,19}$/.test(pwd)
}

export function parseTime(date, fmt){  
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}